home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / othergnu / gdbm15.zoo / update.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-24  |  3.5 KB  |  121 lines

  1. /* update.c - The routines for updating the file to a consistent state. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32.  
  33.  
  34. /* This procedure writes the header back to the file described by DBF. */
  35.  
  36. static 
  37. write_header (dbf)
  38.      gdbm_file_info *dbf;
  39. {
  40.   int  num_bytes;    /* Return value for write. */
  41.   long file_pos;    /* Return value for lseek. */
  42.  
  43.   file_pos = lseek (dbf->desc, 0, L_SET);
  44.   if (file_pos != 0) _gdbm_fatal (dbf, "lseek error");
  45.   num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  46.   if (num_bytes != dbf->header->block_size)
  47.     _gdbm_fatal (dbf, "write error");
  48.  
  49. #ifndef atarist
  50.   /* Wait for all output to be done. */
  51.   fsync (dbf->desc);
  52. #endif
  53. }
  54.  
  55.  
  56. /* After all changes have been made in memory, we now write them
  57.    all to disk. */
  58. _gdbm_end_update (dbf)
  59.      gdbm_file_info *dbf;
  60. {
  61.   int  num_bytes;    /* Return value for write. */
  62.   long file_pos;    /* Return value for lseek. */
  63.   
  64.   
  65.   /* Write the current bucket. */
  66.   if (dbf->bucket_changed)
  67.     {
  68.       _gdbm_write_bucket (dbf, dbf->cache_entry);
  69.       dbf->bucket_changed = FALSE;
  70.     }
  71.  
  72.   /* Write the other changed buckets if there are any. */
  73.   if (dbf->second_changed)
  74.     {
  75.       int index;
  76.  
  77.       for (index = 0; index < CACHE_SIZE; index++)
  78.     if (dbf->bucket_cache[index].ca_changed)
  79.       {
  80.         _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
  81.       }
  82.       dbf->second_changed = FALSE;
  83.     }
  84.   
  85.   /* Write the directory. */
  86.   if (dbf->directory_changed)
  87.     {
  88.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  89.       if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, "lseek error");
  90.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  91.       if (num_bytes != dbf->header->dir_size)
  92.     _gdbm_fatal (dbf, "write error");
  93.       dbf->directory_changed = FALSE;
  94. #ifndef atarist
  95.       if (!dbf->header_changed) fsync (dbf->desc);
  96. #endif
  97.     }
  98.  
  99.   /* Final write of the header. */
  100.   if (dbf->header_changed)
  101.     {
  102.       write_header (dbf);
  103.       dbf->header_changed = FALSE;
  104.     }
  105. }
  106.  
  107.  
  108. /* If a fatal error is detected, come here and exit. VAL tells which fatal
  109.    error occured. */
  110. _gdbm_fatal (dbf, val)
  111.      gdbm_file_info *dbf;
  112.      char *val;
  113. {
  114.   if (dbf->fatal_err != NULL)
  115.     (*dbf->fatal_err) (val);
  116.   else
  117.     fprintf (stderr, "gdbm fatal: %s.\n", val);
  118.   exit (-1);
  119. }
  120.  
  121.